import { $get } from '@js/request'
import { colors } from '@js/config'
import { ConditionSchema } from '@src/components/schema/condition'

export interface ItemSchema {
  name: string
  value: number
  color: string
  percent: string
}

export async function getDataRequest (condition: ConditionSchema[], dataType: string): Promise<ItemSchema[]> {
  let key = 'casenum'
  const data: { [k: string]: any } = {
    order: key,
    condition: JSON.stringify(condition),
    gfield: 'procedure'
  }
  if (dataType !== '案件数') {
    key = 'disputeamount'
    data.order = key
  }
  if (dataType === '总标的') {
    data.dispute_amount_type = 'dispute_amount_all'
  } else if (dataType === '本请求') {
    data.dispute_amount_type = 'dispute_amount'
  } else if (dataType === '反请求') {
    data.dispute_amount_type = 'counter_dispute_amount'
  }
  const res = await $get('/api/commission/casestat/case_field', data)
  const list: Array<{ name: string, value: number }> = res.data[key]
  let total = 0
  let tp = 100
  list.forEach(li => { total += li.value })
  return list.map((li, i: number) => {
    let p = (li.value / total * 100).toFixed(2)
    if (i === list.length - 1) {
      p = tp.toFixed(2)
    } else {
      tp = tp - Number(p)
    }
    return {
      ...li,
      color: colors[i],
      percent: p
    }
  })
}
